import React, { Component } from 'react'
import { Form, Button, Switch, Radio } from 'antd'
import { messages } from "share/common"
import Condition from 'components/condition'
import Priority from "components/template/priority"
import constant from "../constant";
import SaveHoc from "./save-hoc";

class PrintRule extends Component {

  state = {
    priority: null,
    saveLoading: false
  };

  componentWillMount() {
    const { record } = this.props.params;
    this.setState({
      priority: record.optionId ? {
        levelOrgName: record.levelOrgName,
        levelOrgId: record.levelOrgId,
        distribution: record.isGlobal ? null : {
          companies: record.distributionList.map(item => { return { name: item.levelOrgName, id: item.levelOrgId } })
        }
      } : null
    })
  };

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        this.props.handleSave('savePrintRule', value, 'distributionList');
      }
    })
  };

  handlePriority = value => {
    this.setState({ priority: value });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { saveLoading, priority } = this.state;
    const { close } = this.props, { record } = this.props.params;
    const { formItemLayout } = constant;
    return (
      <div>
        <Form className={'new-form'}>
          <Condition>
            <Condition.If value={record.onlyRead}>
              <h3 className='sub-title'>{messages('finance-1.key431')/*借款单打印*/}</h3>
              <Form.Item
                label={messages('finance-1.key432')/*打印模板*/}
                {...formItemLayout}
                required
              >
                <div>{messages('finance-1.key247')/*多行模板*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key249')/*打印方向*/}
                {...formItemLayout}
                required
              >
                <div>{messages('finance-1.key250')/*横向*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key252')/*借款行信息*/}
                {...formItemLayout}
                extra={messages('finance-1.key433')/*打印文件中显示「借款行信息」区域*/}
                required
              >
                <div>{messages('common.print')/*打印*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key254')/*银行信息*/}
                {...formItemLayout}
                extra={messages('finance-1.key434')/*打印文件中加密显示收款银行信息*/}
                required
              >
                <div>{messages('finance-1.key255')/*加密*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key62')/*审批历史*/}
                {...formItemLayout}
                extra={messages('finance-1.key435')/*打印文件中显示「审批历史」区域*/}
                required
              >
                <div>{messages('common.print')/*打印*/}</div>
              </Form.Item>
            </Condition.If>
            <Condition.Else>
              <Form.Item
                label={messages('common.column.status')/*状态*/}
                {...formItemLayout}
              >
                {getFieldDecorator("isEnable", {
                  rules: [{ required: true }],
                  valuePropName: "checked",
                  initialValue: record.hasOwnProperty('isEnable') ? record.isEnable : true
                })(
                  <Switch>{messages('finance-1.key244')/*启用temp*/}</Switch>
                )}
              </Form.Item>
              <Priority
                top='SET_OF_BOOK'
                disabledTop={record.hasOwnProperty('optionId')}
                onChange={this.handlePriority}
                formItemLayout={formItemLayout}
                defaultValue={priority}
                single={false}
                showCompany={false}
              />
              <h3 className='sub-title'>{messages('finance-1.key431')/*借款单打印*/}</h3>
              <Form.Item
                label={messages('finance-1.key432')/*打印模板*/}
                {...formItemLayout}
              >
                {getFieldDecorator("optionPrintTemp", {
                  rules: [{ required: true }],
                  initialValue: record.optionPrintTemp || '1'
                })(
                  <Radio.Group options={[
                    { label: messages('finance-1.key247')/*多行模板*/, value: '1' },
                    { label: messages('finance-1.key248')/*单行模板*/, value: '2' }
                  ]}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key249')/*打印方向*/}
                {...formItemLayout}
              >
                {getFieldDecorator("optionPrintDrc", {
                  rules: [{ required: true }],
                  initialValue: record.optionPrintDrc || '1'
                })(
                  <Radio.Group options={[
                    { label: messages('finance-1.key250')/*横向*/, value: '1' },
                    { label: messages('finance-1.key251')/*纵向*/, value: '2' }
                  ]}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key252')/*借款行信息*/}
                {...formItemLayout}
                extra={messages('finance-1.key436')/*多行模版的打印文件中显示「借款行信息」区域*/}
              >
                {getFieldDecorator("optionWithLine", {
                  rules: [{ required: true }],
                  initialValue: record.optionWithLine || '1'
                })(
                  <Radio.Group options={[
                    { label: messages('common.print')/*打印*/, value: '1' },
                    { label: messages('finance-1.key253')/*不打印*/, value: '2' }
                  ]}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key254')/*银行信息*/}
                {...formItemLayout}
                extra={messages('finance-1.key437')/*多行模版的打印文件中加密显示收款银行信息*/}
              >
                {getFieldDecorator("optionEncryptedBc", {
                  rules: [{ required: true }],
                  initialValue: record.optionEncryptedBc || '1'
                })(
                  <Radio.Group options={[
                    { label: messages('finance-1.key255')/*加密*/, value: '1' },
                    { label: messages('finance-1.key256')/*不加密*/, value: '2' }
                  ]}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key62')/*审批历史*/}
                {...formItemLayout}
                extra={messages('finance-1.key435')/*打印文件中显示「审批历史」区域*/}
              >
                {getFieldDecorator("optionWithAppHist", {
                  rules: [{ required: true }],
                  initialValue: record.optionWithAppHist || '1'
                })(
                  <Radio.Group options={[
                    { label: messages('common.print')/*打印*/, value: '1' },
                    { label: messages('finance-1.key253')/*不打印*/, value: '2' }
                  ]}/>
                )}
              </Form.Item>
            </Condition.Else>
          </Condition>
        </Form>
        <div className='footer-operate'>
          <Condition.If value={!record.onlyRead}>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
          </Condition.If>
          <Button onClick={() => close()}>{record.onlyRead ? messages('common.back')/*返回*/ : messages('common.cancel')/*取消*/}</Button>
        </div>
      </div>
    )
  }

}

export default Form.create()(SaveHoc(PrintRule));
